package com.hyc.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.hyc.entity.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TokenService {
    @Value("tokenKey")
    private String tokenKey;

    private static final long EXPIRE_TIME= 20*1000;

    public String generateToken(User user) {
        Date expiresAt = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        return JWT.create().withAudience(user.getId().toString(), user.getName()).withExpiresAt(expiresAt)
                .sign(Algorithm.HMAC256(tokenKey));
    }
}
